<script setup>

import {
  House,
  SetUp,
  Connection,
  ChatDotRound,
  TrophyBase,
  User,
  Calendar,
  Stamp,
  Bell,
  CaretBottom,
  Crop,
  Grid,
  Ticket,
  SwitchButton,
  EditPen,
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import {useRouter} from 'vue-router'
const router = useRouter()
import {userLogoutService} from "@/api/user";
import {useTokenStore} from "@/stores/token.js"
import {getUserInfoService} from "@/api/user";
// import {useRouter} from "vue-router";
import {ElMessage, ElMessageBox} from "element-plus";
import useUserInfoStore from "@/stores/userInfo.js";
const userInfoStore = useUserInfoStore()
const tokenStore = useTokenStore()
//调用后台退出登录接口
// const logout = async()=>{
//   console.log(123);

//     router.push('/login')
// }
// import {useTokenStore} from "@/stores/token";

// const userInfoStore = useUserInfoStore();
// const getUserInfo = async () => {
//   const result = await userInfoService();
//   userInfoStore.setUserInfo(result.data)
// }
// getUserInfo()

// const router = useRouter();
// const tokenStore = useTokenStore();
const handleCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm(
        '你确认要退出吗？',
        '温馨提示',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }
    ).then(
        async () => {
          let result = await userLogoutService();
          ElMessage({
              showClose: true,
              message: result.description?result.description:"退出登录",
              type: 'success',
          })
          // clear data in pinia
          // userInfoStore.removeUserInfo()
          // tokenStore.removeToken()
          // ElMessage.success("退出成功")
          tokenStore.removeToken()
          console.log(tokenStore.token);
          
          router.push('/login')
        }
    )
  } else {
    router.push( command)
  }
}
//调用函数，获取用户详细信息
const getUserInfo = async () => {
  const result = await getUserInfoService();
  console.log(result.data);
  userInfoStore.setUserInfo(result.data)
  console.log(userInfoStore.userInfo);
}
getUserInfo()


</script>

<template>
  <el-container class="layout-container">
    <!-- 左侧菜单 -->
    <el-aside width="200px">
      <div class="el-aside__logo"></div>
      <el-menu active-text-color="#ffd04b" background-color="#232323" text-color="#fff"
               router>
        <el-menu-item index="/index">
          <el-icon>
            <House />
          </el-icon>
          <span>主页</span >
        </el-menu-item>

        <el-menu-item index="/department">
          <el-icon>
            <SetUp />
          </el-icon>
          <span>部门管理</span >
        </el-menu-item>

        <el-menu-item index="/user">
          <el-icon>
            <TrophyBase />
          </el-icon>

          <span>员工管理</span >
        </el-menu-item>

        <el-menu-item index="/leave">
          <el-icon>
            <Stamp />
          </el-icon>
          <span>请假管理</span >
        </el-menu-item>

        <el-menu-item index="/attendance">
          <el-icon>
            <Calendar />
          </el-icon>
          <span>考勤管理</span >
        </el-menu-item>
       
        <el-menu-item index="/expense">
          <el-icon><Ticket /></el-icon>
          <span>报销管理</span >
        </el-menu-item>
       

        <el-menu-item index="/task">
          <el-icon>
              <Connection/>
            </el-icon>
          <span>任务管理</span >
        </el-menu-item>
       

        <el-menu-item index="/message">
          <el-icon>
              <ChatDotRound/>
            </el-icon>
          <span>留言管理</span >
        </el-menu-item>

        <el-menu-item index="/notice">
          <el-icon>
              <Bell/>
            </el-icon>
          <span>公告管理</span >
        </el-menu-item>
        
        <el-sub-menu>
          <template #title>
            <el-icon>
              <User/>
            </el-icon>
            <span>个人中心</span>
          </template>
          <el-menu-item index="/info">
            <el-icon><Grid /></el-icon>
            <span>基本资料</span>
          </el-menu-item>
          <el-menu-item  index="/user/avatar">
            <el-icon>
              <Crop/>
            </el-icon>
            <span>更换头像</span>
          </el-menu-item>
          <el-menu-item index="/user/resetPassword">
            <el-icon>
              <EditPen/>
            </el-icon>
            <span>重置密码</span>
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>
    <!-- 右侧主区域 -->
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <div>管理员：<strong>{{userInfoStore.userInfo.userName}}</strong></div>
        <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-avatar :src="userInfoStore.userInfo.avatarUrl? userInfoStore.userInfo.avatarUrl:avatar"/>
                        <el-icon>
                            <CaretBottom/>
                        </el-icon>
                    </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
              <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item>
              <el-dropdown-item command="resetPassword" :icon="EditPen">重置密码</el-dropdown-item>
              <el-dropdown-item command="logout" :icon="SwitchButton" @click="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>
      <!-- 中间区域 -->
      <el-main>
        <router-view/>
      </el-main>
      <!-- 底部区域 -->
      <el-footer>oa办公系统 ©2025 Created by 阿尤</el-footer>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;

  .el-aside {
    background-color: #232323;

    &__logo {
      height: 120px;
      background: url('@/assets/logo2.png') no-repeat center / 120px auto;
    }

    .el-menu {
      border-right: none;
    }
  }

  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-dropdown__box {
      display: flex;
      align-items: center;

      .el-icon {
        color: #999;
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }

  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}
</style>